<template>
  <div class="page-container">
    <div class="page-content">
      <!--搜索-->
      <div class="panel-container">
        <searchuinew :searchFormArr="searchFormArr" @searchParma="searchParma" :isAccording="isAccording"></searchuinew>
      </div>
      <!--数据信息-->
      <div class="panel-container no-margin">
        <div class="panel-line flex-end">
          <el-button size="mini" type="primary" @click="handleBatchApply">批量申领</el-button>
        </div>
        <el-table :data="dataList" size="mini" :header-cell-style="{ background: '#F5F5F5', color: '#000000' }" ref="table" style="width: 100%" v-loading="dataListLoading" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50"> </el-table-column>
          <el-table-column align="center" header-align="center" label="序号" type="index" width="100"></el-table-column>
          <el-table-column align="center" header-align="center" label="企业名称" prop="company_name" width="200"></el-table-column>
          <el-table-column align="center" header-align="center" label="联系人" prop="company_boss" width="200"></el-table-column>
          <el-table-column align="center" header-align="center" label="联系电话" prop="company_phone" width="200"></el-table-column>
          <el-table-column align="center" header-align="center" label="意向标签" prop="talk_sts" width="200"></el-table-column>
          <el-table-column align="center" header-align="center" label="外呼任务名称" prop="task_name" width="250"></el-table-column>
          <el-table-column align="center" header-align="center" label="通话开始时间" prop="talk_start_time" width="200"></el-table-column>
          <el-table-column align="center" fixed="right" header-align="center" label="操作" width="200">
            <template slot-scope="scope">
              <el-button size="mini" v-if="scope.row.applyStatus === 0" type="text" @click="handleApply(scope)">申领</el-button>
              <el-button size="mini" v-else type="text" class="grey">已申领</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          small
          background
          :current-page="pageIndex"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="totalPage"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { apiApplyCustomList, apiApplyCustom } from '@/api/customer'
export default {
  name: 'index',
  data() {
    return {
      searchFormArr: [
        {
          fieldName: '企业名称：',
          type: 'input',
          field: 'company_name',
          placeholder: '请输入企业名称',
        },
        {
          fieldName: '联系人：',
          type: 'input',
          field: 'company_boss',
          placeholder: '请输入联系人',
        },
        {
          fieldName: '联系电话：',
          type: 'input',
          field: 'company_phone',
          placeholder: '请输入联系电话',
        },
        {
          fieldName: '意向标签：',
          type: 'select',
          field: 'talk_sts',
          option: [
            { id: 'A', text: 'A' },
            { id: 'B', text: 'B' },
            { id: 'C', text: 'C' },
            { id: 'D', text: 'D' },
            { id: 'E', text: 'E' },
            { id: 'F', text: 'F' },
          ],
          placeholder: '请选择意向标签',
          multiple: true,
        },
        {
          fieldName: '外呼任务名称：',
          type: 'input',
          field: 'task_name',
          placeholder: '请输入外呼任务名称',
        },
        {
          fieldName: '创建时间',
          type: 'timePicker',
          field: 'time',
          placeholder: '请选择创建时间',
          timeType: 'daterange',
          format: 'yyyy-MM-dd',
        },
      ],
      isAccording: true,
      searchParams: {
        company_name: '',
        company_boss: '',
        company_phone: '',
        talk_sts: '',
        task_name: '',
        start_time: '',
        end_time: '',
      },

      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      multipleSelection: [],
    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const info = Object.assign({ pageNumber: this.pageIndex, pageSize: this.pageSize, sts: this.tabActive }, this.searchParams)
      apiApplyCustomList(info)
        .then((res) => {
          const { items, total } = res.result
          items.forEach((item) => {
            item.applyStatus = 0
          })
          this.dataList = items
          this.totalPage = total.total_count
          this.dataListLoading = false
        })
        .catch((e) => {
          this.dataList = []
          this.totalPage = 0
          this.dataListLoading = false
        })
    },
    // 列表查询
    searchParma(e) {
      console.log(e)
      this.searchFormArr.forEach((item) => {
        if (item.field === 'time') return
        if (e[item.field]) {
          this.searchParams[item.field] = e[item.field]
        } else {
          this.searchParams[item.field] = ''
        }
      })
      if (e.time) {
        this.searchParams.start_time = e.time[0]
        this.searchParams.end_time = e.time[1]
      }
      if (e.talk_sts && e.talk_sts.length > 0) {
        this.searchParams.talk_sts = e.talk_sts.join(',')
      } else {
        this.searchParams.talk_sts = ''
      }
      this.pageIndex = 1
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 申领
    handleApply(scope) {
      this.applyCustom([scope.row.id])
    },
    // 批量申领
    handleBatchApply() {
      if (this.multipleSelection.length <= 0) {
        this.$message({
          type: 'error',
          message: '请至少选择一项数据!',
        })
        return
      }
      this.applyCustom(this.multipleSelection)
    },
    applyCustom(id) {
      this.dataListLoading = true
      apiApplyCustom(id).then(() => {
        this.dataListLoading = false
        this.$message({
          type: 'success',
          message: '申领成功!',
        })
        id.forEach((item) => {
          this.dataList.find((data) => {
            return data.id === item
          }).applyStatus = 1
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map((item) => {
        return item.id
      })
    },
  },
}
</script>

<style scoped lang="scss">
.panel-line {
  margin-bottom: 16px;
}
.grey {
  color: #999999;
}
</style>
